/* 弧度转角度 */
const r2d = (r: number) => (r * Math.PI) / 180;

/* 角度转弧度 */
const d2r = (d: number) => (d * 180) / Math.PI;

/**
 * 根据宽高，获取对应圆的半径
 * @param w 宽度
 * @param h 高度
 * @returns 角度
 */
const deriveRadiusFromWH = (w: number, h: number) => Math.sqrt(w ** 2 + h ** 2);

/**
 * 将目标数字保留两位小数
 * @param n 目标数字
 */
const toFixed = (n: number) => Number(n.toFixed(2));

/** 根据半径，和旋转角度，获取圆上的点
 * @param {number} radius 圆的半径
 * @param {number} deg 角度
 */

type PDParams = {
  radius: number;
  deg: number;
  w: number;
  h: number;
};
const getPointByDeg = (params: PDParams) => {
  const x = toFixed(params.radius * Math.cos(r2d(params.deg)));
  const y = toFixed(params.radius * Math.sin(r2d(params.deg)));

  return { x, y };
};

/**
 * 根据坐标计算角度
 * @param {number} x
 * @param {number} y
 */
const calculateAngle = (y: number, x: number): number => (Math.atan2(y, x) * 180) / Math.PI;

type P = {
  x: number;
  y: number;
};

/**
 *
 * @param {P} startPointer : 圆上某点(初始点)
 * @param {P} centerPointer : 圆心
 * @param {P} angle : 旋转角度
 * @returns
 */
const rotatePoint = (startPointer: P, centerPointer: P, angle: number) => {
  const { x: a, y: b } = centerPointer;
  const { x: x0, y: y0 } = startPointer;
  const rx = a + (x0 - a) * Math.cos(r2d(angle)) - (y0 - b) * Math.sin(r2d(angle));
  const ry = b + (x0 - a) * Math.sin(r2d(angle)) + (y0 - b) * Math.cos(r2d(angle));
  return { x: rx, y: ry };
};

export { toFixed, getPointByDeg, r2d, calculateAngle, d2r, deriveRadiusFromWH, rotatePoint };
